<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/5/17
 * Time: 16:28
 */
namespace Mengwu\Controller;

use Mengwu\Api\MwApi;

class AccountController extends BaseStoreController{

    private $appid = 'wx673ef65eb4a7c8ff';
    private $weixin = '';

    protected function _initialize(){
        parent::_initialize();
        $redirect = I('get.redirect');
        $redirect = urlencode($redirect);
        $redirect_uri = urlencode(C('SITE_URL').U('mengwu/account/weixin_login',array('redirect'=>$redirect)));
        $state = md5(session_id());
        $this->weixin = "https://open.weixin.qq.com/connect/qrconnect?appid=$this->appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_login&state=$state#wechat_redirect";
        $this->assign('weixin_login',$this->weixin);
    }


    /**
     * 已登陆，跳转到user
     */
    protected function jump2user(){
        if($this->isLogin()) redirect(U('mengwu/user/index'));
    }

    public function register(){

        $this->jump2user();
        $this->assignTitle('注册');
        if(IS_POST){
            $username = I('post.account','');
            $password = I('post.psw','');
            $code = I('post.code','');
            $invite_code = I('post.invite_code','');
            $data = [
                'api_ver'  => 101,
                'reg_type' => 3,  //手机号注册
                'username' => $username,
                'password' => $password,
                'from'     => 0,  //来自内部应用登录
                'code'     => $code,
                'invite_code' => $invite_code
            ];

            $result = mwApi(MwApi::BY_User_register,$data);
            $this->apiReturn($result,'注册成功');
        }

        $this->boye_display();

    }

    public function login(){

        $this->jump2user();
        $this->assignTitle('登陆');
        if(IS_POST){
            $username = I('post.account','');
            $password = I('post.psw','');
            $redirect_uri = I('get.redirect');

            $data = [
                'username' => $username,
                'password' => $password
            ];

            $result = mwApi(MwApi::BY_User_login,$data);
            if($result['status']){
                session('user',$result['info']);
            }
            $this->apiReturn($result,['info'=>'登陆成功','redirect_uri'=>$redirect_uri]);
        }else{

            if(empty($redirect_uri)){
                $redirect_uri = $_SERVER['HTTP_REFERER'];
                if(empty($redirect_uri)) $redirect_uri = C('SITE_URL');
                $this->assign('redirect_uri',$redirect_uri);
            }
        }

        $this->boye_display();

    }

    public function logout(){

        if(IS_AJAX){
            if($this->isLogin()){
                session('user',null);
                $this->ajaxReturnSuc('退出成功');
            }else{
                $this->ajaxReturnSuc('您没有登陆');
            }
        }
    }

    /**
     * 发送注册验证码
     */
    public function send_register(){
        
        if(IS_POST){
            $mobile = I('post.account',0,'int');
            if(empty($mobile)) $this->ajaxReturnErr('错误的手机号码');

            $data = [
                'mobile' => $mobile,
                'code_type' => 1
            ];

            $result = mwApi(MwApi::BY_Message_send_sms,$data);

            if($result['status']){
                $this->ajaxReturnErr('验证码已发送~'.$result['info']);
            }else{
                $this->ajaxReturnErr($result['info']);
            }

        }
        
    }

    /**
     * 微信登陆
     */
    public function weixin_login(){

        $code = I('code');
        $state = I('state');
        $redirect = urldecode(I('redirect'));

        if($state===md5(session_id())){

        }else{
            exit('error state');
        }

        if(empty($code)){
            exit('error code');
        }

        $data = [
            'code' => $code,
            'from' => 'pc'
        ];

        $result = mwApi(MwApi::BY_Weixin_login,$data);

        if($result['status']){
            session('user',$result['info']);
            if(!empty($redirect)){
                redirect($redirect);
            }else{
                redirect(C('SITE_URL'));
            }
        }else{
            $this->errorPage($result['info']);
        }

    }
}